package so.wy.servicecenter.businessbed.service.pay;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ManagerException;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.service.AbstractService;
import so.wy.servicecenter.businessbed.dao.enums.StatusUserDepositEnum;
import so.wy.servicecenter.businessbed.dao.rdb.rent_user.RentUserDO;
import so.wy.servicecenter.businessbed.manager.inner.RentUserIManager;
import so.wy.servicecenter.businessbed.manager.normal.WxPayOptManager;

@Service
@Scope("prototype")
public class PayOptService extends AbstractService {
    @Autowired
    private WxPayOptManager wxPayOptManager;

    @Autowired
    private RentUserIManager rentUserIManager;

    public void doWithDraw(Long rentUserId) throws ServiceException {
        try {
            RentUserDO rentUserDO = rentUserIManager.checkRentUserExistById(rentUserId);

            rentUserIManager.checkRentUserWithDraw(rentUserId);

            wxPayOptManager.rechargeTopUpPrePay(rentUserDO);
        } catch (ManagerException exception) {
            throw exception(exception.getMessage());
        }
    }

    //押金充值
    public WxPayMpOrderResult yjczPre(Long userId) throws ServiceException {
        try {
            RentUserDO rentUserDO = rentUserIManager.checkRentUserExist(userId);

            if (StatusUserDepositEnum.PAYD.getCode().equals(rentUserDO.getDepositStatus())){
                throw exception("请勿重复充值");
            }

            return wxPayOptManager.createTopUpPrePay(rentUserDO);

        } catch (ManagerException exception) {
            throw exception(exception.getMessage());
        }
    }

    //押金体现
    public void yjtx(Long userId) throws ServiceException {
        try {
            RentUserDO rentUserDO = rentUserIManager.checkRentUserExist(userId);

            rentUserIManager.checkRentUserWithDraw(rentUserDO.getRentUserId());

            wxPayOptManager.rechargeTopUpPrePay(rentUserDO);
        } catch (ManagerException exception) {
            throw exception(exception.getMessage());
        }
    }
}
